package com.book.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.book.core.domain.BookCategories;
import com.book.core.vo.BookCategoryVO;
import com.book.repository.BookCategoriesMapper;
import com.book.service.BookCategoriesService;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author hongmizfb
 * @description 针对表【book_categories(图书分类表)】的数据库操作Service实现
 * @createDate 2025-05-07 18:33:44
 */
@Service
public class BookCategoriesServiceImpl extends ServiceImpl<BookCategoriesMapper, BookCategories>
        implements BookCategoriesService {

    @Override
    public List<BookCategoryVO> getTreeList() {
//        查找所有的分类
        List<BookCategoryVO> allList = allList().stream().map(bookCategory -> {
            BookCategoryVO bookCategoryVO = new BookCategoryVO();
            BeanUtils.copyProperties(bookCategory, bookCategoryVO);
            return bookCategoryVO;
        }).collect(Collectors.toList());
//        筛选出一级分类
        List<BookCategoryVO> parentList = allList.stream().filter(bookCategoryVO -> bookCategoryVO.getParentId() == 0).collect(Collectors.toList());
        return convertToTree(parentList, allList);
    }

    @Cacheable(value = "bookCategory",key = "'allList'")
    public List<BookCategories> allList() {
        return lambdaQuery().list();
    }

    @Override
    public List<BookCategories> getChildrenList(Integer categoryId, Integer level) {
        // 参数校验
        if (categoryId == null || level == null || level < 1 || level > 3) {
            return Collections.emptyList();
        }
        // 如果是第三级，直接返回当前分类的单元素列表
        if (level == 3) {
            BookCategories category = getById(categoryId);
            return category != null ? Collections.singletonList(category) : Collections.emptyList();
        }
        // 如果是第一级或第二级，查询所有直接子分类
        List<BookCategories> children = lambdaQuery()
                .eq(BookCategories::getParentId, categoryId)
                .list();
        // 如果需要递归获取更深层级的子分类（例如原代码可能想实现的功能）
        if (level == 1) {
            List<BookCategories> allChildren = new ArrayList<>(children);
            for (BookCategories child : children) {
                allChildren.addAll(getChildrenList(child.getCategoryId(), level + 1));
            }
            return allChildren;
        }
        return children;
    }

    @Override
    @CachePut(value = "bookCategory",key = "'allList'")
    public void createBookCategory(BookCategories bookCategories) {
        save(bookCategories);
    }

    /**
     * 使用递归实现列表转树状结构
     *
     * @param parentList 父级分类
     * @param allList    所有分类
     * @return {@link List }<{@link BookCategoryVO }>
     */
    public List<BookCategoryVO> convertToTree(List<BookCategoryVO> parentList, List<BookCategoryVO> allList) {
        parentList.forEach(parent -> {
            List<BookCategoryVO> children = allList.stream().filter(child -> child.getParentId().equals(parent.getCategoryId())).collect(Collectors.toList());
            convertToTree(children, allList);
            parent.setChildren(children);
        });
        return parentList;
    }
}




